/* fafamtn.p - FIXED ASSETS MAINTENANCE - Actual Units Per Period           */
/* Copyright 1986 QAD Inc. All rights reserved.                               */
/* $Id:: fafamtn.p 59128 2015-08-21 10:49:10Z nsc                          $: */
/* REVISION: 9.1      LAST MODIFIED: 10/26/99   BY: *N021* Pat Pigatti      */
/* REVISION: 9.1      LAST MODIFIED: 03/24/00   BY: *N08T* Annasaheb Rahane */
/* REVISION: 9.1      LAST MODIFIED: 05/17/00   BY: *M0LJ* Vihang Talwalkar */
/* REVISION: 9.1      LAST MODIFIED: 07/28/00   BY: *N0BX* Arul Victoria    */
/* REVISION: 9.1      LAST MODIFIED: 08/14/00   BY: *N0L0* Jacolyn Neder    */
/* REVISION: 9.1      LAST MODIFIED: 03/13/02   BY: *M1NB* Alok Thacker     */
/* Revision: 1.22.1.12  BY: Kirti Desai        DATE: 05/03/02 ECO: *N1GN*   */
/* Revision: 1.22.1.13  BY: Rajesh Lokre       DATE: 04/03/03 ECO: *M1RX* */
/* Revision: 1.22.1.15  BY: Paul Donnelly (SB) DATE: 06/26/03 ECO: *Q00C* */
/* Revision: 1.22.1.16  BY: Robert Jensen      DATE: 09/02/04 ECO: *N2XK* */
/* Revision: 1.22.1.17  BY: Vandna Rohira      DATE: 12/17/04 ECO: *P2Z5* */
/*-Revision end---------------------------------------------------------------*/

/******************************************************************************/
/* All patch markers and commented out code have been removed from the source */
/* code below. For all future modifications to this file, any code which is   */
/* no longer required should be deleted and no in-line patch markers should   */
/* be added.  The ECO marker should only be included in the Revision History. */
/******************************************************************************/


{us/bbi/mfdeclre.i}
/* EXTERNAL LABEL INCLUDE */
{us/bbi/gplabel.i}

define input parameter p-status as character format "x(25)" no-undo.
define parameter buffer fab_det for fab_det.

/* --------------------  Define Query  ------------------- */
define query q_fabd_det for
   fabd_det
   scrolling.

/* -----------------  Standard Variables  ---------------- */
define variable perform-status as character format "x(25)"
   initial "first" no-undo.
define variable p-skip-update  like mfc_logical        no-undo.
define variable l-rowid        as   rowid              no-undo.
define variable l-delete-it    like mfc_logical        no-undo.
define variable l-del-rowid    as   rowid              no-undo.
define variable l-top-rowid    as   rowid              no-undo.
define variable lines          as   integer initial 10 no-undo.
define variable l-found        like mfc_logical        no-undo.
define variable pos            as   integer            no-undo.
define variable l-first        like mfc_logical        no-undo.
define variable l-error        like mfc_logical        no-undo.
define variable l-title        as   character          no-undo.
define variable l-error-number like msg_nbr            no-undo.

/* ------------------  Button Variables  ----------------- */
define button b-add    label "Add".
define button b-delete label "Delete".
define button b-end    label "End".

/* -------------  Standard Widget Variables  ------------- */
define variable l-focus as widget-handle no-undo.
define variable w-frame as widget-handle no-undo.

/* ------------------  Screen Variables  ----------------- */
define shared variable l-fa-id as character format "x(12)" no-undo.
define variable l-err-nbr as integer format ">>>9"
   initial 0 no-undo.
define variable l-seq as integer format ">>>>>>>>>"
   initial 0 no-undo.
define variable l-per as character no-undo.

define buffer fabd-det for fabd_det.

/* ------------------  Frame Definition  ----------------- */
define frame f-button
   b-add at 1
   b-delete at 10
   b-end at 19
with no-box overlay three-d side-labels width 28.

/* CLEAR FRAME REGISTRATION TO TRANSLATE THE FRAME EVERYTIME*/
clearFrameRegistration(frame f-button:handle).

/* SET EXTERNAL LABELS */
setFrameLabels(frame f-button:handle).

assign
   l-focus               = b-add:handle in frame f-button
   b-add:width           = 8
   b-add:private-data    = "add"
   b-delete:width        = 8
   b-delete:private-data = "delete"
   b-end:width           = 8
   b-end:private-data    = "end".

define frame f-1
   skip(.4)
   space(15)
   fabd_yrper
   fabd_upper
   fabd_accup
with three-d overlay 3 down scroll 1 width 80.

/* CLEAR FRAME REGISTRATION TO TRANSLATE THE FRAME EVERYTIME*/
clearFrameRegistration(frame f-1:handle).

/* SET EXTERNAL LABELS */
setFrameLabels(frame f-1:handle).

run ip-framesetup.

w-frame = frame f-1:handle.
{us/bbi/gprun.i ""fafmtut.p""
   "(w-frame)"}

lines = 03.

if p-status = "add"
then do:
   frame f-1:visible = true.
   return.
end. /* IF p-status = "add" */

/* ---------------  Pre Processing Include  -------------- */
/* VARIABLES FOR GPGLEF1.I */
{us/gp/gpglefv.i}

/* VARIABLES FOR FA PERSISTENT PROCEDURES */
{us/gp/gprunpdf.i "fapl" "p"}

for first fa_mstr
   where fa_mstr.fa_domain = global_domain 
    and  fa_id = l-fa-id
no-lock:
end. /* FOR FIRST fa_mstr */

/* -------------  END Pre Processing Include  ------------ */

open query q_fabd_det
   for each fabd_det
      where fabd_det.fabd_domain = global_domain 
      and   fabd_fa_id   = fab_det.fab_fa_id
      and   fabd_fabk_id = fab_det.fab_fabk_id
      and   fabd_resrv   = 0
      use-index fabd_fa_id no-lock.

get first q_fabd_det no-lock.

main-loop:
do while perform-status <> "end" on error undo:
   if perform-status = ""
   and  p-status     = "first-one"
   then
      return.

   if p-status = "end"
   then
      perform-status = "end".

   if {us/bbi/gpiswrap.i} then frame f-1:visible = yes.

   run ip-query
      (input-output perform-status,
       input-output l-rowid).

   if {us/bbi/gpiswrap.i} then hide frame f-1 no-pause.

   if perform-status = "delete"
   then do:
      run ip-lock
         (input-output perform-status).
      perform-status = "first".
      run ip-predisplay.
   end. /* IF perform-status = "delete" */

   if p-status = "first-one"
   and not available fabd_det
   then do:
      frame f-1:visible = true.
      return.
   end. /* IF p-status = "first-one" */

   /* ----------------------  Display  ---------------------- */
   run ip-predisplay.

   /* ----------------------  Add  ------------------------ */
   if (perform-status = "update"
   or  perform-status = "add")
   then do:

      run ip-prompt.

      if l-error = yes
      then
         return.

      if perform-status = "undo"
      then do:
         perform-status = "first".
         next main-loop.
      end. /* IF perform-status = "undo" */

      run ip-displayedits.
   end. /* IF (perform-status = "update" */

   /* -----------------------  End  ----------------------- */
   if perform-status = "end"
   then do:
      hide frame f-1 no-pause.
      delete PROCEDURE this-procedure no-error.
      leave.
   end. /* IF perform-status = "end" */

   /* ----------------  Selection Buttons  ---------------- */
   if  p-status <> "first-one"
   and p-status <> "end"
   and perform-status <> "first"
   then
      run ip-button
         (input-output perform-status).

   /* -------------  After Strip Menu Include  ------------ */
   if perform-status = "add"
   or perform-status = "delete"
   then do:
      if can-find(fa_mstr
                     where fa_mstr.fa_domain = global_domain 
                     and   fa_id = fab_fa_id
                     and   fa_disp_dt <> ?)
      then do:
         /* MODIFICATIONS NOT ALLOWED - ASSET HAS BEEN RETIRED */
         {us/bbi/pxmsg.i &MSGNUM=3211 &ERRORLEVEL=4}
         perform-status = "".
         next main-loop.
      end. /* IF CAN-FIND(fa_mstr */

      if fab_famtr_id <> ""
      then do:
         /* UNITS CANNOT BE ENTERED WHEN METER IS SET */
         {us/bbi/pxmsg.i &MSGNUM=2992 &ERRORLEVEL=4}
         perform-status = "".
         next main-loop.
      end. /* IF fab_famtr_id <> "" */

      if perform-status = "add"
      and can-find(last fabd_det
                      where fabd_det.fabd_domain = global_domain 
                        and fabd_fa_id = fab_fa_id
                        and fabd_fabk_id = fab_fabk_id
                        and fabd_accup = fab_uplife)
      then do:
         /* ASSET IS FULLY CONSUMED */
         {us/bbi/pxmsg.i &MSGNUM=2991 &ERRORLEVEL=4}
         perform-status = "".
         next main-loop.
      end. /* IF perform-status = "add" */

      if perform-status = "delete"
      then do:

         if not available fabd_det
         then do:
            /* DEPRECIATION SCHEDULE DOES NOT EXIST */
            {us/bbi/pxmsg.i &MSGNUM=3199 &ERRORLEVEL=4}
            perform-status = "".
            next main-loop.
         end. /* IF NOT AVAILABLE fabd_det */

         else
         if can-find(first fabd-det
                        where fabd-det.fabd_domain = global_domain 
                         and (fabd-det.fabd_fa_id = fab_fa_id
                         and  fabd-det.fabd_fabk_id = fab_fabk_id
                         and  fabd-det.fabd_yrper > fabd_det.fabd_yrper))
                          or fabd_det.fabd_post = yes
         then do:
            /* ONLY LAST UNPOSTED PERIOD CAN BE DELETED */
            {us/bbi/pxmsg.i &MSGNUM=2993 &ERRORLEVEL=4}
            perform-status = "".
            next main-loop.
         end. /* IF CAN-FIND(FIRST fabd-det ... */

      end.  /* IF perform-status = "delete" */
   end.  /* IF perform-status = "add" OR "delete" */
   /* -----------  END After Strip Menu Include  ---------- */

end. /* DO WHILE perform-status <> "end"  */

/* -------------  Add / Update / Field Edits  ------------ */
PROCEDURE ip-prompt:

   define variable l-date as date no-undo.

   if perform-status = "add"
   then do:
      scroll from-current down with frame f-1.
      l-rowid = ?.
   end. /* IF perform-status = "add" */

   l-first = yes.

   CASE perform-status:
      when ("add")
      then
         /* ADDING NEW RECORD */
         {us/bbi/pxmsg.i &MSGNUM=1 &ERRORLEVEL=1}
      when ("update")
      then
         /* MODIFYING EXISTING RECORD */
         {us/bbi/pxmsg.i &MSGNUM=10 &ERRORLEVEL=1}
      otherwise
         hide message no-pause.
   END CASE.

   ststatus = stline[3].
   status input ststatus.

   prompt-for-it:
   repeat:

      l-first = no.
      repeat:

         prompt-for
            fabd_det.fabd_yrper when (perform-status = "add")
            fabd_det.fabd_upper
         with frame f-1.

         /* FIELD EDIT FOR fabd_yrper */
         l-error = no.

         /* CHECK IF YEAR PERIOD ENTERED IS A VALID ENTRY */
         {us/gp/gprunp.i "fapl" "p" "fa-chk-input-yrper"
            "(input  frame f-1 fabd_yrper,
              output l-error-number)"}

         if l-error-number <> 0
         then do:
            l-error = yes.
            /*  40: BLANK NOT ALLOWED */
            /* 495: INVALID PERIOD    */
            {us/bbi/pxmsg.i &MSGNUM=l-error-number &ERRORLEVEL=3}
         end. /* IF l-error-number <> 0 */
         else do:

            /* VALIDATE AGAINST CALENDAR */
            for first fabk_mstr
               where fabk_mstr.fabk_domain = global_domain 
                and  fabk_id = fab_det.fab_fabk_id
            no-lock:
            end. /* FOR FIRST fabk_mstr */

            if not available fabk_mstr
            then do:
               /* INVALID BOOK CODE */
               {us/bbi/pxmsg.i &MSGNUM=4214 &ERRORLEVEL=4}
               l-error = yes.
               return.
            end. /* IF NOT AVAILABLE fabk_mstr */

            /* FIND THE START DATE FOR A GIVEN YEAR AND PERIOD */
            {us/gp/gprunp.i "fapl" "p" "fa-conv-per-to-date"
               "(input  fabk_calendar,
                 input  frame f-1 fabd_yrper,
                 output l-date,
                 output l-error-number)"}

            if l-error-number <> 0
            then do:
               l-error = yes.
               /* DATE NOT WITHIN A VALID PERIOD */
               {us/bbi/pxmsg.i &MSGNUM=l-error-number &ERRORLEVEL=3}
            end. /* IF l-error-number <> 0 */

            if  fabk_calendar = ""
            and l-error       = no
            then do:
               /* VALIDATE OPEN GL PERIOD */
               {us/bbi/gprun.i ""gpglef1.p""
                  "(input ""FA"",
                    input fa_mstr.fa_entity,
                    input l-date,
                    output gpglef_result,
                    output gpglef_msg_nbr)"}

               if gpglef_result > 0
               then do:
                  {us/bbi/pxmsg.i &MSGNUM=gpglef_msg_nbr &ERRORLEVEL=3}
                  l-error = yes.
               end. /* IF gpglef_result > 0 */

            end.  /* IF fabk_calender = "" */

            if l-error = no
            then do:
               {us/gp/gprunp.i "fapl" "p" "fa-get-per"
                  "(input  fab_date,
                    input  fabk_calendar,
                    output l-per,
                    output l-err-nbr)"}

               if input frame f-1 fabd_yrper < l-per
               then do:
                  /* YEAR-PERIOD MUST BE EQUAL TO OR GREATER THAN THE */
                  /* SERVICE PERIOD                                   */
                  {us/bbi/pxmsg.i &MSGNUM=2995 &ERRORLEVEL=3}
                  l-error = yes.
               end. /* IF INPUT FRAME f-1 fabd_yrper < l-per */
               else
               if can-find(first fabd_det
                           where fabd_det.fabd_domain = global_domain 
                             and fabd_fa_id = fab_det.fab_fa_id
                             and fabd_fabk_id = fab_fabk_id
                             and fabd_yrper >= input frame f-1 fabd_yrper)
               then do:
                  /* YEAR-PERIOD MUST BE GREATER THAN LAST PERIOD ENTERED */
                  {us/bbi/pxmsg.i &MSGNUM=2994 &ERRORLEVEL=3}
                  l-error = yes.
               end. /* IF CAN-FIND(FIRST fabd_det */

            end.  /* IF l-error = NO */
         end.  /* length(fabd_yrper) = 6 */
         if l-error
         then do:
            next-prompt fabd_yrper with frame f-1.
            next.
         end. /* IF l-error */

         /* END FIELD EDIT FOR fabd_yrper */

         leave.
      end. /* REPEAT */

      if keyfunction(lastkey) = "end-error"
      then do:
         perform-status = "undo".
         return no-apply.
      end. /* IF KEYFUNCTION(LASTKEY) = "END-ERROR" */
      leave.
   end. /* PROMPT-FOR-IT */

   if keyfunction(lastkey) = "end-error"
   then do:

      if perform-status = "add"
      then do:

         /* RECORD NOT ADDED */
         {us/bbi/pxmsg.i &MSGNUM=1304 &ERRORLEVEL=1}
         get first q_fabd_det no-lock.
         if available fabd_det
         then
            assign
               perform-status = "first"
               l-rowid        = rowid(fabd_det).
         else
            assign
               perform-status = ""
               l-rowid        = ?.
         clear frame f-1 no-pause.
      end. /* IF perform-status = "add" */
      else
      if available fabd_det
      then
         assign
            perform-status = ""
            l-rowid        = rowid(fabd_det).
      return.
   end. /* IF KEYFUNCTION(LASTKEY) = "END-ERROR" */

   run ip-lock
      (input-output perform-status).

END PROCEDURE. /* ip-prompt */

/* -----------------------  Locking  ----------------------- */
PROCEDURE ip-lock:
   define input-output parameter perform-status as character no-undo.

   if perform-status = "add"
   or perform-status = "update"
   or perform-status = "delete"
   then
      ip-lock:
      do transaction on error undo:
         assign
            pos         = 0
            l-delete-it = yes.

         if available fabd_det
         then
            get current q_fabd_det no-lock.

         if available fabd_det
         and current-changed fabd_det
         then do:

            l-delete-it = yes.
            /* RECORD HAS BEEN MODIFIED SINCED YOU EDITED IT SAVE ANYWAY */
            {us/bbi/pxmsg.i &MSGNUM=2042 &ERRORLEVEL=1 &CONFIRM=l-delete-it
                     &CONFIRM-TYPE='LOGICAL'}

            if l-delete-it = no
            then do:
               hide message no-pause.
               run ip-displayedits.
               return.
            end. /* IF l-delete-it = NO */
            hide message no-pause.
         end. /* IF AVAILABLE fabd_det */
         if available fabd_det
         then
            tran-lock:
            do while perform-status <> "":

               get current q_fabd_det exclusive-lock no-wait.
               if locked fabd_det
               then do:

                  if pos < 3
                  then do:
                     pos = pos + 1.
                     pause 1 no-message.
                     next tran-lock.
                  end. /* IF pos < 3 */

                  perform-status = "error".
               end. /* IF LOCKED fabd_det */
               leave.
            end. /*while*/

         if (perform-status = "update"
         or  p-status = "update")
         and l-delete-it = yes
         then
            run ip-update
               (input-output perform-status).

         if perform-status = "delete"
         then
            run ip-delete
               (input-output perform-status).

         if available fabd_det
         then
            get current q_fabd_det no-lock.

      end. /* DO TRANSACTION ON ERROR UNDO */

   if perform-status = "add"
   then
      run ip-add
         (input-output perform-status).

END PROCEDURE. /* ip-lock */

PROCEDURE ip-update:

   define input-output parameter perform-status as character no-undo.

   if p-status = "update"
   then
      p-status = "".

   run ip-assign
      (input-output perform-status).

   perform-status = "open".

   run ip-query
      (input-output perform-status,
       input-output l-rowid).

   reposition q_fabd_det to rowid l-rowid no-error.

   get next q_fabd_det no-lock.

   assign
      perform-status = "first"
      l-rowid        = rowid(fabd_det).

   return.

END PROCEDURE. /* ip-update */

PROCEDURE ip-add:
   define input-output parameter perform-status as character no-undo.

   /* REMOVED FOURTH PARAMETER l-acq */
   {us/bbi/gprun.i ""faupblb.p""
      "(buffer fa_mstr,
        buffer fab_det,
        input  input frame f-1 fabd_yrper)"}

   create fabd_det. 
   fabd_det.fabd_domain = global_domain.
   run ip-assign
      (input-output perform-status).

   perform-status = "open".

   run ip-query
      (input-output perform-status,
       input-output l-rowid).

   reposition q_fabd_det to rowid l-rowid no-error.

   get next q_fabd_det no-lock.

   assign
      perform-status = "first"
      l-rowid        = rowid(fabd_det).
   return.
END PROCEDURE. /* ip-add */

PROCEDURE ip-delete:

   define input-output parameter perform-status as character no-undo.

   l-delete-it = no.

   /* PLEASE CONFIRM DELETE */
   {us/bbi/pxmsg.i &MSGNUM=11 &ERRORLEVEL=1 &CONFIRM=l-delete-it
            &CONFIRM-TYPE='LOGICAL'}

   CASE l-delete-it:
      when yes
      then do:

         hide message no-pause.
         delete fabd_det.
         clear frame f-1 no-pause.
         get next q_fabd_det no-lock.

         if available fabd_det
         then
            assign
               perform-status = "first"
               l-rowid = rowid(fabd_det).
         else do:
            get prev q_fabd_det no-lock.
            if available fabd_det
            then
               assign
                  perform-status = "first"
                  l-rowid = rowid(fabd_det).
            else
               assign
                  perform-status = "first"
                  l-rowid = rowid(fabd_det).
         end. /* ELSE DO */

         /* RECORD DELETED */
         {us/bbi/pxmsg.i &MSGNUM=22 &ERRORLEVEL=1}
         return.
      end. /* WHEN YES */
      otherwise
         run ip-displayedits.
   END CASE.
END PROCEDURE. /* ip-delete */

PROCEDURE ip-assign:

   define input-output parameter perform-status as character no-undo.

   do with frame f-1:
      assign
         fabd_det.fabd_yrper
         fabd_det.fabd_upper
         fabd_det.fabd_mod_userid = global_userid
         fabd_det.fabd_mod_date   = today
         fabd_det.fabd_fa_id      = fab_det.fab_fa_id
         fabd_det.fabd_fabk_id    = fab_det.fab_fabk_id
         fabd_det.fabd_adj_yrper  = fabd_det.fabd_yrper
         fabd_det.fabd_entity     = fa_mstr.fa_entity
         fabd_det.fabd_faloc_id   = fa_mstr.fa_faloc_id
         fabd_det.fabd_facls_id   = fa_mstr.fa_facls_id
         l-rowid                  = rowid(fabd_det).

      /* --------  /* AFTER-ASSIGN-AUDIT-INCLUDE */  ------- */
      {us/mf/mfnxtsq1.i  "fabd_det.fabd_domain = global_domain and " fabd_det
         fabd_seq fabd_sq01 l-seq}
      fabd_seq = l-seq.
      {us/bbi/gprun.i ""faupbla.p""
         "(""not-meter-add"",
           """",
           fabd_upper,
           ?,
           rowid(fabd_det),
           output l-err-nbr)"}

      if l-err-nbr <> 0
      then do:
         /* DISPLAY MSG BACK FROM faupbla.p */
         {us/bbi/pxmsg.i &MSGNUM=l-err-nbr &ERRORLEVEL=3}
      end. /* IF l-err-nbr <> 0 */

      perform-status = "open".

      run ip-query
         (input-output perform-status,
          input-output l-rowid).

      run ip-predisplay.
      /* ------  END /* AFTER-ASSIGN-AUDIT-INCLUDE */  ----- */

   end. /* DO WITH FRAME f-1 */
END PROCEDURE. /* ip-assign */

PROCEDURE ip-predisplay:
   if (perform-status = ""
    or perform-status = "update")
   and available fabd_det
   then
      display-loop:
      do:

         if perform-status = ""
         then do:
            clear frame f-1 all no-pause.
            l-top-rowid = rowid(fabd_det).
         end. /* IF perform-status = "" */

         read-loop:
         do pos = 1 to lines:
            if perform-status = ""
            then do:
               if session:display-type = "gui"
               then
                  assign
                     fabd_yrper:bgcolor = 8
                     fabd_yrper:fgcolor = 0.
               else
                  color display normal fabd_yrper
                  with frame f-1.
            end. /* IF perform-status = "" */

            run ip-displayedits.

            if perform-status = "update"
            then
               leave display-loop.

            if pos < lines
            then
               down with frame f-1.

            get next q_fabd_det no-lock.

            if not available fabd_det
            then
               leave.
         end. /* DO pos = 1 TO lines */

         run ip-postdisplay.
      end. /* DO */
END PROCEDURE. /* ip-predisplay */

PROCEDURE ip-displayedits:

   if available fabd_det
   then
      run ip-display.

END PROCEDURE. /* ip-dispayedits */

PROCEDURE ip-display:

   display
      fabd_det.fabd_yrper
      fabd_det.fabd_upper
      fabd_det.fabd_accup
   with frame f-1.
END PROCEDURE. /* ip-display */

PROCEDURE ip-postdisplay:

   do pos = 1 to lines:
      if frame-line(f-1) <= 1
      then
         leave.
      up 1 with frame f-1.
   end. /* DO pos = 1 TO lines */

   if perform-status = ""
   then do:

      reposition q_fabd_det to rowid l-top-rowid no-error.
      get next q_fabd_det no-lock.
      l-rowid = l-top-rowid.

      if p-status <> "first-one"
      then
         color display message fabd_yrper
         with frame f-1.
   end. /* IF perform-status = "" */
END PROCEDURE. /* ip-postdisplay */

PROCEDURE ip-query:

   define input-output parameter perform-status as character no-undo.
   define input-output parameter l-rowid as rowid no-undo.

   if perform-status = "first"
   then do:

      if l-rowid <> ?
      then do:
         reposition q_fabd_det to rowid l-rowid no-error.
         get next q_fabd_det no-lock.
      end. /* IF l-rowid <> ? */

      if not available fabd_det
      then
         get first q_fabd_det no-lock.

      if available fabd_det
      then
         assign
            perform-status = ""
            l-rowid        = rowid(fabd_det).
      else do:
         assign
            perform-status    = ""
            l-rowid           = ?
            frame f-1:visible = true.
         return.
      end. /* ELSE DO */

   end. /* IF perform-status = "FIRST" */

   if perform-status = "open"
   then do:
      open query q_fabd_det
         for each fabd_det
            where fabd_det.fabd_domain = global_domain 
              and fabd_fa_id = fab_det.fab_fa_id
              and fabd_fabk_id = fab_det.fab_fabk_id
              and fabd_resrv   = 0
            use-index fabd_fa_id no-lock.

      reposition q_fabd_det to rowid l-rowid no-error.

      get next q_fabd_det no-lock.

      if available fabd_det
      then
         assign
            perform-status = ""
            l-rowid        = rowid(fabd_det).
      else do:
         get first q_fabd_det no-lock.
         if not available fabd_det
         then do:
            assign
               perform-status    = ""
               frame f-1:visible = true.
            return.
         end. /* IF NOT AVAILABLE fabd_det */
         else
            assign
               perform-status = ""
               l-rowid        = rowid(fabd_det).
      end. /* ELSE DO */
   end. /* IF perform-status = "open" */

   if perform-status = "next"
   then do:

      get next q_fabd_det no-lock.
      if available fabd_det
      then do:

         hide message no-pause.
         assign
            l-rowid        = rowid(fabd_det)
            perform-status = "next".

         if session:display-type = "gui"
         then
            assign
               fabd_yrper:bgcolor = 8
               fabd_yrper:fgcolor = 0.
         else
            color display normal fabd_yrper
            with frame f-1.

         pause 0.  /* MAKES SCROLLING WORK - DON'T REMOVE */
         down 1 with frame f-1.

         run ip-displayedits.
         color display message fabd_yrper
         with frame f-1.
      end. /* IF AVAILABLE fabd_det */

      else do:
         perform-status = "next".
         /* END OF FILE */
         {us/bbi/pxmsg.i &MSGNUM=20 &ERRORLEVEL=2}
         get last q_fabd_det no-lock.
      end. /* ELSE DO */
   end. /* IF perform-status = "next" */

   if perform-status = "prev"
   then do:

      get prev q_fabd_det no-lock.
      if not available fabd_det
      then do:
         /* BEGINNING OF FILE */
         {us/bbi/pxmsg.i &MSGNUM=21 &ERRORLEVEL=2}
         get first q_fabd_det no-lock.
         return.
      end. /* IF NOT AVAILABLE fabd_det */

      hide message no-pause.
      l-rowid = rowid(fabd_det).

      if session:display-type = "gui"
      then
         assign
            fabd_yrper:bgcolor = 8
            fabd_yrper:fgcolor = 0.
      else
         color display normal fabd_yrper
         with frame f-1.

      pause 0.  /* MAKES SCROLLING WORK - DON'T REMOVE */
      up 1 with frame f-1.

      run ip-displayedits.
      color display message fabd_yrper
      with frame f-1.
   end. /* IF perform-status = "prev" */

   if perform-status = "update"
   or perform-status = "delete"
   then do:

      get current q_fabd_det no-lock.
      if not available fabd_det
      then do:

         hide message no-pause.
         /* RECORD NOT FOUND */
         {us/bbi/pxmsg.i &MSGNUM=1310 &ERRORLEVEL=3}
         perform-status = "".
      end. /* IF NOT AVAILABLE fabd_det */
   end. /* IF perform-status = "update" ... */

END PROCEDURE. /* ip-query */

PROCEDURE ip-framesetup:

   if session:display-type = "gui"
   then
      assign
         current-window:bgcolor = 8
         frame f-1:row          = 12
         frame f-1:overlay      = true.
   else
      assign
         frame f-1:row          = 11.

   assign
      frame f-1:box           = true
      frame f-1:centered      = true
      frame f-1:title         = (getFrameTitle("DEPRECIATION_SCHEDULE",30))
      frame f-button:centered = true
      frame f-button:row      = 20.
END PROCEDURE. /* ip-framesetup */

PROCEDURE ip-button:

   define input-output parameter perform-status as character
      format "x(25)" no-undo.

   if not batchrun
   then do:

      enable all with frame f-button.
      ststatus = stline[2].
      status input ststatus.

      on choose of b-add
      do:
         assign
            perform-status = self:private-data
            l-focus        = self:handle.
         hide frame f-button no-pause.
      end. /* DO */

      on choose of b-delete
      do:
         assign
            perform-status = self:private-data
            l-focus        = self:handle.
         hide frame f-button no-pause.
      end. /* DO */

      on choose of b-end
      do:
         assign
            perform-status = self:private-data
            l-focus        = self:handle.
         hide frame f-1 no-pause.
         hide frame f-button no-pause.
      end. /* DO */

      on cursor-up, f9 anywhere
      do:
         assign
            perform-status = "prev"
            l-focus        = focus:handle.
         apply "go" to frame f-button.
      end. /* DO */

      on cursor-down, f10 anywhere
      do:
         assign
            perform-status = "next"
            l-focus        = focus:handle.
         apply "go" to frame f-button.
      end. /* DO */

      on cursor-right anywhere
      do:
         l-focus = self:handle.
         apply "tab" to l-focus.
      end. /* DO */

      on cursor-left anywhere
      do:
         l-focus                 = self:handle.
         if session:display-type = "gui"
         then
            apply "shift-tab" to l-focus.
         else
            apply "ctrl-u" to l-focus.
      end. /* DO */

      on f3 anywhere
         apply "choose" to b-add in frame f-button.
      on f5, ctrl-d anywhere
         apply "choose" to b-delete in frame f-button.
      on esc anywhere
      do:
         if session:display-type = "gui"
         then
            apply "choose" to b-end in frame f-button.
      end. /* DO */

      on end-error anywhere
         apply "choose" to b-end in frame f-button.
      on window-close of current-window
         apply "choose" to b-end in frame f-button.
      on go anywhere
      do:
         if (lastkey = keycode("cursor-down")
         or  lastkey = keycode("cursor-up")
         or  lastkey = keycode("f9")
         or  lastkey = keycode("f10"))
         then
            return.
         else
            l-focus = focus:handle.
         apply "choose" to l-focus.
      end. /* DO */

      wait-for
         go of frame f-button or
         window-close of current-window or
         choose of
         b-add,
         b-delete,
         b-end
         focus
         l-focus.

   end. /* IF NOT BATCHRUN */
   else
      set perform-status.

   hide message no-pause.
END PROCEDURE. /* ip-button */
